
package com.meme.editor.level;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.util.ListIterator;

import com.meme.editor.Box2d;
import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;
import com.meme.editor.level.define.DefineFile;
import com.meme.editor.model.sprite2d.AnimatedModule;
import com.meme.editor.model.sprite2d.AnimatedModuleGroup;
import com.meme.editor.property.PropertyFactory;
import com.meme.io.editor.FileCache;
import com.meme.util.FileUtil;
import com.meme.util.MiscUtil;

public class CircleAppearance extends Appearance 
{
	public static final String ParamName_BorderColor = "border_color";
	public static final String ParamName_FilledColor = "filled_color";
	public static final String ParamName_Radius = "radius";
	public static final String ParamName_CenterX = "center_x";
	public static final String ParamName_CenterY = "center_y";
	
	
	final static String GroupName_BoxGroup = "Circle Properties";
	final static String PropertyName_Radius        = "Radius";
	final static String PropertyName_CenterX       = "Center X";
	final static String PropertyName_CenterY       = "Center Y";

	
	Color mBorderColor = new Color (0);
	Color mFilledColor = null;
	int mRadius = 5;
	
	int mCenterX = 0;
	int mCenterY = 0;
	
	boolean mRelativeToActor = true;
	boolean mApplyActorScaleAndFlips = false;
	
	public CircleAppearance ()
	{
		super ();
		
		createPropertyGroup (GroupName_BoxGroup);
		createPublicProperty (GroupName_BoxGroup, PropertyName_Radius, PropertyFactory.ValueType_Number);
		createPublicProperty (GroupName_BoxGroup, PropertyName_CenterX, PropertyFactory.ValueType_Number);
		createPublicProperty (GroupName_BoxGroup, PropertyName_CenterY, PropertyFactory.ValueType_Number);
	}

	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{			
		super._handleModifyingPropertyValues (modications);
		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if ( mod.mGroupName.equals (GroupName_BoxGroup) )
			{
				boolean handled = true;
				if ( mod.mPropertyName.equals (PropertyName_Radius) )
				{
					SetRadius (( (Float) (mod.mNewValue) ).intValue());
				}
				else if ( mod.mPropertyName.equals (PropertyName_CenterX) )
				{
					SetCenterX (( (Float) (mod.mNewValue) ).intValue());
				}
				else if ( mod.mPropertyName.equals (PropertyName_CenterY) )
				{
					SetCenterY (( (Float) (mod.mNewValue) ).intValue());
				}
				else
					handled = false;
				
				if (handled)
					iter.remove();
			}
		}
	}	
	
	@Override
	public void setParameter (String paramName, String paramValue)
	{
		if (paramName.equals(ParamName_BorderColor))
		{
			mBorderColor = MiscUtil.parseColor(paramValue, 0x0);
		}
		else if (paramName.equals(ParamName_FilledColor))
		{
			mFilledColor = MiscUtil.parseColor(paramValue, 0x60ffffff);
		}
		else if (paramName.equals(ParamName_Radius))
		{
			SetRadius ( MiscUtil.parseInt(paramValue, 0x0) );
		}
		else if (paramName.equals(ParamName_CenterX))
		{
			SetCenterX ( MiscUtil.parseInt(paramValue, 0x0) );
		}
		else if (paramName.equals(ParamName_CenterY))
		{
			SetCenterY ( MiscUtil.parseInt(paramValue, 0x0) );
		}
	}
	
	public void SetRadius (int radius)
	{
		if (radius < 0)
			radius = 0;
		
		mRadius = radius;

		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_Radius, mRadius);
	}
	
	public int getRadius ()
	{
		return mRadius;
	}

	public void SetCenterX (int x)
	{
		mCenterX = x;

		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_CenterX, mCenterX);
	}
	
	public int getCenterX ()
	{
		return mCenterX;
	}

	public void SetCenterY (int y)
	{
		mCenterY = y;

		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_CenterY, mCenterY);
	}
	
	public int getCenterY ()
	{
		return mCenterY;
	}

//	@Override
//	public void show (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flag)
//	{
//		Color oldColor = g.getColor ();
//
//		if (mFilledColor != null)
//		{
//			g.setColor (mFilledColor);
//			g.fillRect(posX + mLeft, posY + mTop, mRight - mLeft, mBottom - mTop);
//		}
//		
//		g.setColor (mBorderColor);
//		g.drawRect(posX + mLeft, posY + mTop, mRight - mLeft, mBottom - mTop);
//		
//		g.setColor (oldColor);
//	}

	@Override
	public void show (Graphics2D g, int flag)
	{
		Color oldColor = g.getColor ();

		if (mFilledColor != null)
		{
			g.setColor (mFilledColor);
			
			g.fillArc(mCenterX - mRadius, mCenterY - mRadius, mRadius + mRadius, mRadius + mRadius, 0, 360);
		}
		
		g.setColor (mBorderColor);
		g.drawArc(mCenterX - mRadius, mCenterY - mRadius, mRadius + mRadius, mRadius + mRadius, 0, 360);
		
		g.setColor (oldColor);
	}

	@Override
	public void getBoundingBox (Box2d box2d)
	{
		box2d.set(mCenterX - mRadius, mCenterY - mRadius, mCenterX + mRadius, mCenterY + mRadius);
		
//		Actor actor = (Actor) getParent ();
//		if (mApplyActorScaleAndFlips)
//			box2d.adjust (actor.isFlipX (), actor.isFlipY ());
//		if (mRelativeToActor)
//			box2d.shift(actor.getPosX(), actor.getPosY());
	}
	
	
	@Override
	public boolean isGeometryAppearance ()
	{
		return true;
	}
}
